package it.unibo.deis.lia.mmhc.spm;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.BindException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Vector;

public class ServiceClient extends Thread {

	private Vector<String> goHop;
	

	private Vector<String> backHop;
	int port;
	private String fornitore;
	private String richiedente;
	private boolean go = true;
	private String name;
	private SocketVector socketVector;

	public ServiceClient(String name, Vector<String> goHop,
			Vector<String> backHop, int port, String fornitore,
			String richiedente) {
		super();
		this.goHop = goHop;
		this.backHop = backHop;
		this.port = port;
		this.fornitore = fornitore;
		this.richiedente = richiedente;
		this.name = name;
		socketVector= SocketVector.getInstance();
		

	}

	public void run() {
		try {
			ServerSocket ss = null;
			try {
				ss = new ServerSocket(port, 0, InetAddress.getLocalHost());
				System.err.println("Aperto il client sulla porta " + port);
			} catch (BindException e) {
				go = false;
			}

			while (true) {
				System.err.println("preaccept");
				Socket input = ss.accept();
				socketVector.addSocket("127.0.0.1", input.getPort(), input);
new ServiceClientThread(input,this).start();
				
				// out.close();
				// input.close();
			}
		} catch (Exception e) {
			System.err.println("Service client " + e);
		}

	}
	public Vector<String> getGoHop() {
		return goHop;
	}

	public Vector<String> getBackHop() {
		return backHop;
	}

	public int getPort() {
		return port;
	}

	public String getFornitore() {
		return fornitore;
	}

	public String getRichiedente() {
		return richiedente;
	}

	public boolean isGo() {
		return go;
	}

	public String getName2() {
		return name;
	}
}
